<template>
  <div class="modal fade" :id="id" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h6 class="modal-title">改签完成</h6>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <div class="card">
            <div class="card-body">
              <form>
                <div class="row mb-1">
                  <label class="col-form-label col-3 text-end"> 新编码 </label>
                  <div class="col-9">
                    <input
                      type="text"
                      class="form-control form-control-sm"
                      v-model.trim="pnrNo"
                    />
                  </div>
                </div>
                <div class="row mb-1">
                  <label class="col-form-label col-3 text-end"> 新票号 </label>
                  <div class="col-9">
                    <input
                      type="text"
                      class="form-control form-control-sm"
                      v-model.trim="ticketNo"
                    />
                  </div>
                </div>
                <div class="row mb-1">
                  <label class="col-form-label col-3 text-end"> </label>
                  <div class="col-9">
                    <label class="form-check-label">
                      <input
                        type="checkbox"
                        class="form-check-input"
                        v-model="confirmed"
                      />
                      确定
                    </label>
                    <span class="text-muted small"
                      >填写准确或不填，勾选后再确定</span
                    >
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";

export default {
  props: {
    id: {
      type: String,
      default: "modalPrompt",
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      ticketNo: "",
      pnrNo: "",
      confirmed: false,

      myModal: null
    };
  },
  methods: {
    ok: function () {
      if (this.confirmed === false) return;

      this.hideModal();

      this.resolve({
        ticketNo: this.ticketNo,
        pnrNo: this.pnrNo,
      });
    },
    modal: function () {
      this.ticketNo = "";
      this.pnrNo = "";
      this.confirmed = false;


      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
